package com.xingongchang.baolebei.protocol;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import com.external.activeandroid.Model;
import com.external.activeandroid.annotation.Column;
import com.external.activeandroid.annotation.Table;

@Table(name = "USER")
public class USER extends Model {

	@Column(name = "collection_num")
	public String collection_num;

	@Column(name = "USER_id", unique = true)
	public int Id;

	@Column(name = "order_num")
	public ORDER_NUM order_num;

	@Column(name = "rank_name")
	public String rank_name;

	@Column(name = "rank_level")
	public int rank_level;

	@Column(name = "name")
	public String name;
	
	@Column(name = "nickname")
	public String nickname;
	
	@Column(name = "user_photo")
	public String user_photo;
	

	public static USER fromJson(JSONObject jsonObject) throws JSONException {
		if (null == jsonObject) {
			return null;
		}

		USER localItem = new USER();

		JSONArray subItemArray;

		localItem.collection_num = jsonObject.optString("collection_num");

		localItem.Id = Integer.parseInt(jsonObject.optString("id"));
		localItem.order_num = ORDER_NUM.fromJson(jsonObject
				.optJSONObject("order_num"));

		localItem.name = jsonObject.optString("name");
		localItem.rank_name = jsonObject.optString("rank_name");
		localItem.rank_level = jsonObject.optInt("rank_level");
		localItem.nickname = jsonObject.optString("nickname");
		localItem.user_photo = jsonObject.optString("user_photo");
		return localItem;
	}

	public JSONObject toJson() throws JSONException {
		JSONObject localItemObject = new JSONObject();
		JSONArray itemJSONArray = new JSONArray();
		localItemObject.put("collection_num", collection_num);
		localItemObject.put("id", Id);
		if (null != order_num) {
			localItemObject.put("order_num", order_num.toJson());
		}
		localItemObject.put("name", name);
		localItemObject.put("rank_name", rank_name);
		localItemObject.put("rank_level", rank_level);
		localItemObject.put("nickname", nickname);
		localItemObject.put("user_photo", user_photo);
		return localItemObject;
	}

}
